package com.fsy.utils;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * Author : MIAOHY
 * time : 2021/1/26
 * 封装的返回结果类
 */
@Data
@Builder
@AllArgsConstructor
    public class Result11<T> implements Serializable {
        /**
         * 响应码
         */
        private int code;
        /**
         * 响应消息
         */
        private String msg;
        /**
         * 数据
         */
        private T data;
        /**
         * 响应时间
         */
        @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date time =new Date();

        public Result11() {

        }

        public static Result11 result11(ResultCode resultCode, Object data){
            return result11(resultCode,null,data);
        }

        public static Result11 result11(ResultCode resultCode, String msg, Object data){
            boolean success = false;
            if (resultCode.getCode() == ResultCode.SUCCESS.getCode()){
                success = true;
            }
            String message = resultCode.getMsg();
            if (StrUtil.isNotBlank(msg)){
                message = msg;
            }
            return Result11.builder()
                    .code(resultCode.getCode())
                    .msg(message)
                    .data(data)
                    .time(new Date())
                    .build();
        }

        public static Result11 ok(Object data){
            return result11(ResultCode.SUCCESS,data);
        }
        public static Result11 ok(Object data, String msg){
            return result11(ResultCode.SUCCESS,msg,data);
        }

        public static Result11 fail(Object data){
            return result11(ResultCode.FAIL,data);
        }
        public static Result11 fail(Object data, String msg){
            return result11(ResultCode.FAIL,msg,data);
        }

    }